home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 3.8 KB | 157 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPriMem.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWPRIMEM_H
- #include "FWPriMem.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #define FW_OPENDOC 1
-
- #if FW_OPENDOC && defined(FW_BUILD_MAC)
- #include <MemMgr.h>
- #endif
-
- #if FW_OPENDOC && defined(FW_BUILD_WIN)
- #include <MMStubs.h>
- #endif
-
- // ----- Platform Includes -----
-
- #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
- #include <Windows.h>
- #endif
-
-
- #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
- #include <WindowsX.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
- #include <Memory.h>
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWCommon
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrimitiveAllocateBlock
- //----------------------------------------------------------------------------------------
- FW_FUNC_ATTR void* FW_PrimitiveAllocateBlock(size_t n)
- {
- #if FW_OPENDOC
- return MMAllocate(n);
- #elif defined(FW_BUILD_MAC)
- return NewPtr(n);
- #elif defined(FW_BUILD_WIN)
- return GlobalAllocPtr(GHND, n);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrimitiveResizeBlock
- //----------------------------------------------------------------------------------------
- FW_FUNC_ATTR void* FW_PrimitiveResizeBlock(void* p,
- size_t n)
- {
- #if FW_OPENDOC
- return MMReallocate(p, n);
- #elif defined(FW_BUILD_MAC)
- void* q = p;
- if (q==NULL)
- q = FW_PrimitiveAllocateBlock(n);
- else
- {
- SetPtrSize((Ptr) q, n);
- if (MemError() != noErr)
- {
- q = FW_PrimitiveAllocateBlock(n);
- if (q)
- {
- BlockMoveData(p, q, n);
- FW_PrimitiveFreeBlock(p);
- }
- }
- }
- return q;
- #elif defined(FW_BUILD_WIN)
- return GlobalReAllocPtr(p, n, 0);
- #endif
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrimitiveGetBlockSize
- //----------------------------------------------------------------------------------------
- size_t FW_FUNC_ATTR FW_PrimitiveGetBlockSize(void* p)
- {
- #if FW_OPENDOC
- return MMBlockSize(p);
- #elif defined(FW_BUILD_MAC)
- return GetPtrSize((Ptr) p);
- #elif defined(FW_BUILD_WIN)
- return GlobalSize(GlobalPtrHandle(p));
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrimitiveFreeBlock
- //----------------------------------------------------------------------------------------
- void FW_FUNC_ATTR FW_PrimitiveFreeBlock(void* p)
- {
- #if FW_OPENDOC
- if (p)
- MMFree(p);
- #elif defined(FW_BUILD_MAC)
- if (p)
- DisposePtr((Ptr) p);
- #elif defined(FW_BUILD_WIN)
- if (p)
- GlobalFreePtr(p);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrimitiveCopyMemory
- //----------------------------------------------------------------------------------------
-
- void FW_FUNC_ATTR FW_PrimitiveCopyMemory(const void *source, void *destination, size_t bytes)
- {
- #if defined(FW_BUILD_MAC)
- ::BlockMoveData(source, destination, bytes);
- #elif defined(FW_BUILD_WIN)
- ::MoveMemory(destination, source, bytes);
- #else
- // More efficient code is desirable. Don't want to use c standard library.
- // This code must work correctly for overlapping blocks.
- if (source > destination)
- {
- char *dst = (char *) destination;
- const char *src = (const char *) source;
- while (bytes--)
- *dst++ = *src++;
- }
- else if (source < destination)
- {
- char *dst = (char *) destination + bytes;
- const char *src = (const char *) source + bytes;
- while (bytes--)
- *--dst = *--src;
- }
- #endif
- }
-